package com.ksyun.campus.dataserver.util;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class DiskCapacityUtil {

    public static Map<String,Long> getCapacity(){
        File[] roots = File.listRoots();

        //只考虑第一块磁盘
        File root = roots[0];

        log.info("磁盘根目录：" + root.getAbsolutePath());
        Map<String,Long> capacity=new HashMap<>();
        long totalSpace = 0;
        long usableSpace = 0;
        try {
            Path path = root.toPath();
            FileStore fileStore = Files.getFileStore(path);

            totalSpace = fileStore.getTotalSpace();
            usableSpace = fileStore.getUsableSpace();

            log.info("总容量：" + totalSpace / (1024 * 1024) + " MB");
            log.info("可用容量：" + usableSpace / (1024 * 1024) + " MB");
        } catch (IOException e) {
            e.printStackTrace();
        }
        capacity.put("totalCapacity",totalSpace);
        capacity.put("usableCapacity",usableSpace);
        return capacity;
    }
}
